<template>
    <div class="container">
        <!-- 头部 -->
        <el-row>
            <div class="row_Left">
                <div class="line"></div>
                <div class="title">销售统计</div>
                <el-date-picker v-model="begin" type="month" placeholder="选择开始时间" style="width: 150px"
                    value-format="YYYY-MM" class="className"></el-date-picker>
                <p>-</p>
                <el-date-picker v-model="end" type="month" placeholder="选择结束时间" style="width: 150px"
                    value-format="YYYY-MM" class="className"></el-date-picker>
                <el-button @click="search" type="primary" class="search" >
                    <el-icon style="margin-right:5px">
                        <Search />
                    </el-icon>
                    搜索
                </el-button>
            </div>
        </el-row>
        <el-table :data="formData" style="width: 90%;margin: 0 auto;border-radius: 20px;" >
            <el-table-column v-if="roleId==1" prop="userName" label="姓名" align="center" />
            <el-table-column prop="totalSignUpNum" label="总录入人数" align="center" />
            <el-table-column prop="totalTransformNum" label="总转入人数" align="center" />
            <el-table-column prop="totalChange" label="总转化率" align="center" />
        </el-table>
    </div>
</template>

<script setup>
import { ref,onMounted, reactive } from 'vue';
import {salesCount} from '@/components/api/shop'
let search =function(){
    list()
}
let begin=ref("");
let end=ref('');
let roleId=sessionStorage.getItem('roleId')
let formData=ref([])
// 3. 初始化默认日期为当前月份
const initDefaultDate = () => {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始，补0为两位数
    begin.value = `${year}-${month}`;
    end.value = `${year}-${month}`;
};
let list=()=>{
    const data=reactive({
        beginMonth:begin.value,
        endMonth:end.value
    })
    if (roleId == '5') {
      data.userId =Number(sessionStorage.getItem('userId'))
    }
    salesCount(data).then((res)=>{
        console.log(res.dataset[0].totalChange);
        if(res.code==200){
            formData.value = res.dataset || [];
            // 若需要单独处理转化率（如接口返回null），可遍历数组处理
            formData.value.forEach(item => {
                item.change = item.change || 0; // 空值时默认0
            });
            console.log(formData.value);
            
        }
    })
}

onMounted(() => {
    initDefaultDate()
    list()
})
</script>

<style lang="less" scoped>
.container {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    /* 添加容器定位设置，使模态框相对于此容器定位 */
    position: relative;
    z-index: 1;
    /* 确保容器本身在合理层级 */
}

.el-row {
    width: 90%;
    height: 10%;
    margin: 0 auto;
    background-color: #fff;
    margin-bottom: 20px;
    margin-top: 20px;
    box-shadow: 4px 4px 4px rgba(0, 0, 0, 0.1);
    border-radius: 20px;
    justify-content: space-between;
    align-items: center;
    min-height: 71px;
    min-width: 1000px;
}

.row_Left {
    margin-left: 40px;
    display: flex;
    align-items: center;
}

.line {
    width: 2px;
    height: 20px;
    background-color: #007bff;
}

.title {
    padding-left: 10px;
    font-size: 20px;
    margin-right: 20px;
}

.seachName {
    width: 150px;
}

.search {
    background-color: #0f6ab1;
    color: #fff;
    border: none;
    margin-left: 20px;
    border-radius: 16px;
    padding: 0;
    box-sizing: border-box;
    padding-right: 15px;
    padding-left: 16px;
}
</style>